#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os, re
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CheckShaderName(CheckBase):
    def __init__(self):
        super(CheckShaderName, self).__init__()

    def init_file(self):
        import elsLibs.AssetInfoFromFile as elsaif
        reload(elsaif)
        current_file = mu.sceneName()
        aif = elsaif.AssetInfoFromFile(current_file)
        
        self.asset_name = aif.get_name
        self.shortType = aif.get_shortType
        self.step = aif.get_step

    # 检查材质是否跳过名称检查，如果跳过则返回True，相反则返回False
    def check_shader_pass(self, shader):
        pass_shader_names = ["lambert1", "shaderGlow1", "particleCloud1", "displacement", "lgt_"]
        for pass_name in pass_shader_names:
            if re.search(pass_name, shader):
                return True
        return False

    # 检查sg是否跳过名称检查，如果跳过则返回True，相反则返回False
    def check_sg_pass(self, sg):
        pass_sg_names = ["initialParticleSE", "initialShadingGroup", "displacement", "lgt_"]
        for pass_name in pass_sg_names:
            if re.search(pass_name, sg):
                return True
        return False

    def shader_name_check(self):
        miss_nodes = []
        # 检查shader名称
        for node in cmds.ls(type="aiStandardSurface"):
            pass_key = self.check_shader_pass(node)
            if pass_key:
                continue
            if not re.search("{0}_{1}_.+_mat\Z".format(self.shortType, self.asset_name), node):
                miss_nodes.append(node)

        # 检查SG名称
        for node in cmds.ls(type="shadingEngine"):
            pass_key = self.check_sg_pass(node)
            if pass_key:
                continue
            if not re.search("{0}_{1}_.+_matSG\Z".format(self.shortType, self.asset_name), node):
                miss_nodes.append(node)

        # 检查texture 和 utilities节点名称
        check_types = ["aiAdd", "place2dTexture", "file"]
        for node_type in check_types:
            for node in cmds.ls(type=node_type):
                if not re.search("{0}_{1}_.+".format(self.shortType, self.asset_name), node):
                    miss_nodes.append(node)

        return miss_nodes

    def check(self):
        self.init_file()
        miss_shader_nodes = self.shader_name_check()
        if miss_shader_nodes:
            result_log = u'Error:以下Shader节点未匹配对应的名称: %s\n' % miss_shader_nodes
        else:
            result_log = u'Succeed:没有问题\n'
        return result_log

    def fix(self):
        pass

